//
//  AddOtherFeeVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/20.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AddOtherFeeVC.h"
#import "AddOtherFeeDetailVC.h"
#import "AddOtherFeeCell.h"

@interface AddOtherFeeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) AddOtherFeeCell *addOtherFeeCell;
//@property (nonatomic,strong) NSMutableArray *arrListData;
@property (nonatomic,strong) CJLabel *labMoney;
@property (nonatomic,assign) float sumMoney;
@property (nonatomic,strong) CJButton *btnSave;
@end

@implementation AddOtherFeeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"其他费用")];
    [self setBackLeftButton:@""];
    [self setRightImage:CJLString(@"添加") image:@""];
    self.addOtherFeeCell = [[AddOtherFeeCell alloc]initWithFlex:nil reuseIdentifier:nil];
    
//    if (self.arrList.count > 0) {
//        CJSaveMyDefault(@"AddOtherFeeList", self.arrList);
//    }
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.btnSave];
    [self createTableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.arrList.count > 0) {
        [self getSumMOney];
    }
}

- (void)RightTextClick:(UIButton *)sender{

    WS(ws);
    AddOtherFeeDetailVC *vc = [AddOtherFeeDetailVC new];
    vc.strCode = self.strCode;
//    vc.arrList = self.arrList;
    vc.blockSuccess = ^(id anyObject) {

        DLog(@"======= %@",anyObject);
        [ws.arrList addObject:anyObject];
        // 0 收入 1 支出
        [ws.tableView reloadData];
        [ws getSumMOney];
    };
    [self pushVc:vc];
}

- (void)getSumMOney{
    WS(ws);
    self.sumMoney = 0;
    [self.arrList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dic = obj;
        
        if ([dic[@"extraPayType"] intValue] == 0) {
            ws.sumMoney = ws.sumMoney + [obj[@"extraFeeAmount"] floatValue];
        }else{
            ws.sumMoney = ws.sumMoney - [obj[@"extraFeeAmount"] floatValue];
        }
    }];
    
    self.labMoney.text = CJStringWithFormat(@"%@",@(self.sumMoney).cj_money_description);
}
/// 创建tableview 等
- (void)createTableView{
    
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0);
    FlexFrameView *header = [[FlexFrameView alloc] initWithFlex:@"AddOtherFeeHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    [header layoutIfNeeded];
    
//    FlexFrameView *headerFooter = [[FlexFrameView alloc] initWithFlex:@"AddOtherFeeFooterView" Frame:rcFrame Owner:self];
//    headerFooter.flexibleHeight = YES;
//
//    [headerFooter layoutIfNeeded];
    self.tableView.tableHeaderView = header;
//    self.tableView.tableFooterView = headerFooter;
}

- (void)btnSaveInfoAction{
    
    if (self.blockSuccess) {
        self.blockSuccess(self.labMoney.text, self.arrList);
        [self pop];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"AddOtherFeeCell";
    AddOtherFeeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[AddOtherFeeCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    WS(ws);
    NSDictionary *dic = self.arrList[indexPath.row];
    [cell setCellInfoWithDic:dic];

    cell.blockDelete = ^(id anyObject) {
        [ws deletOtherFeeAtIndexPath:indexPath];
    };
    cell.blockEdtior = ^(id anyObject) {
        AddOtherFeeDetailVC *vc = [[AddOtherFeeDetailVC alloc]init];
        vc.strIndex = CJStringWithFormat(@"%ld",(long)indexPath.row);
        vc.dicData = self.arrList[indexPath.row];
        vc.isEditor = YES;
        vc.blockEditor = ^(id anyObject, NSDictionary *dic) {
    
            [ws.arrList replaceObjectAtIndex:[anyObject intValue] withObject:dic];
            [ws.tableView reloadData];
        };
        [ws pushVc:vc];
    };
    return cell;
}

- (void)deletOtherFeeAtIndexPath:(NSIndexPath *)indexPath{
    
    WS(ws);
    [self showAlertView:@"删除提醒" strContent:@"确定要删除该费用吗？" ClickOnSure:^(NSString *strSure) {
        [ws.arrList removeObjectAtIndex:indexPath.row];
        [ws.tableView reloadData];
        [ws getSumMOney];
    } ClickOnCancle:^(NSString *strCancle) {
        
    }];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight + kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  -CJTableToTopHeight - CJBottomBtnHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        ;
    }
    return _tableView;
}

- (CJButton *)btnSave{
    if (!_btnSave) {
        _btnSave = CJButton.btnInit
        .btnFrame(CGRectMake(0, self.tableView.bottom,kScreenWidth, CJBottomBtnHeight))
        .btnBgColor([UIColor cjMainColor])
        .btnText(@"保存")
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnFontSize(17)
        .btnTargetAction(self,@selector(btnSaveInfoAction))
        ;
    }
    return  _btnSave;
}

//- (NSMutableArray *)arrListData{
//    if (!_arrListData) {
//        _arrListData = [[NSMutableArray alloc]init];
//    }
//    return _arrListData;
//}
@end
